<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="位置编码" prop="psCode">
      <!--<el-input v-model="dataForm.psCode" placeholder="位置编码"></el-input>-->
      <el-select v-model="dataForm.psCode" filterable placeholder="位置编码">
        <el-option
          v-for="item in positions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="批次编码" prop="batchNo">
      <el-input v-model="dataForm.batchNo" placeholder="批次编码"></el-input>
    </el-form-item>
    <el-form-item label="物料条码" prop="itemGuid">
      <el-select v-model="dataForm.itemGuid" filterable @change="itemChange" placeholder="物料条码">
        <el-option
          v-for="item in items"
          :key="item.value"
          :label="item.label"
          :value-key="{value:item.value,specId:item.specId}"
          :value="{value:item.value,specId:item.specId}">
        </el-option>
      </el-select>
      <!--<el-input v-model="dataForm.itemGuid" placeholder="物料条码"></el-input>-->
    </el-form-item>
    <el-form-item label="规格ID" prop="specId">
      <el-input v-model="dataForm.specId" :disabled="true" placeholder="规格ID"></el-input>
    </el-form-item>
      <el-form-item label="生产日期" prop="pdDate">
        <el-date-picker
          v-model="dataForm.pdDate"
          type="date"
          placeholder="生产日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerOptions1"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        pickerOptions1:{
          disabledDate(time) {
            return time.getTime() > Date.now();
          }
        },
        visible: false,
        dataForm: {
          id: 0,
          psCode: '',
          batchNo: '',
          itemGuid: '',
          specId: '',
          pdDate: '',
          createTime: '',
          createUser: ''
        },
        positions:[],
        items:[],
        dataRule: {
          psCode: [
            { required: true, message: '位置编码不能为空', trigger: 'blur' }
          ],
          batchNo: [
            { required: true, message: '批次编码不能为空', trigger: 'blur' }
          ],
          itemGuid: [
            { required: true, message: '物料条码不能为空', trigger: 'blur' }
          ],
          specId: [
            { required: true, message: '规格ID不能为空', trigger: 'blur' }
          ],
          pdDate: [
            { required: true, message: '生产日期不能为空', trigger: 'blur' }
          ]

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.listPositionSelectSpec()
        this.listItemSelectSpec()
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/stock/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.psCode = data.stock.psCode
                this.dataForm.batchNo = data.stock.batchNo
                this.dataForm.itemGuid = data.stock.itemGuid
                this.dataForm.specId = data.stock.specId
                this.dataForm.pdDate = data.stock.pdDate
              }
            })
          }
        })
      },
      itemChange(target){
        // console.log(target)
        // console.log(this.dataForm)
        this.dataForm.itemGuid=target.value
        this.dataForm.specId=target.specId
      },
      listPositionSelectSpec () {
        this.$http({
          url: this.$http.adornUrl('/generator/position/listForSelect')
        }).then(({data})=>{
          this.positions = data.data;
        })
      },
      listItemSelectSpec () {
        this.$http({
          url: this.$http.adornUrl('/generator/item/listForSelect')
        }).then(({data})=>{
          this.items = data.data;
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/stock/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'psCode': this.dataForm.psCode,
                'batchNo': this.dataForm.batchNo,
                'itemGuid': this.dataForm.itemGuid,
                'specId': this.dataForm.specId,
                'pdDate': this.dataForm.pdDate,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
